<template>
  <div class="sys-register">
    <div class="register-wrap">
      <div class="register-head">
        <div class="register-title">注册</div>
        <div class="register-sub-title">System Management Center</div>
      </div>
      <div class="register-input">
        <el-form ref="user-input" :rules="userInputRules" :model="userInput" label-width="80px">
          <el-form-item prop="userName" label="用户名">
            <el-input
              v-model="userInput.userName"
              placeholder="请输入用户名"
              prefix-icon="el-icon-user"
              size="medium"
              clearable
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="passWord" label="密码">
            <el-input
              v-model="userInput.passWord"
              placeholder="请输入用户密码"
              prefix-icon="el-icon-lock"
              size="medium"
              clearable
              type="password"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="checkPass" label="确认密码">
            <el-input
              v-model="userInput.checkPass"
              placeholder="请输入用户密码"
              prefix-icon="el-icon-lock"
              size="medium"
              clearable
              type="password"
            >
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="to-register"><span @click="toLogin">已有账号，跳转登录</span></div>
      <el-button type="warning" class="register-btn" @click="toRegister">注册</el-button>
    </div>
  </div>
</template>

<script>
import { register } from '@/api/user'
export default {
  name: 'Register',
  data() {
    var validateUserName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'))
      } else {
        if (this.userInput.checkPass !== '') {
          this.$refs['user-input'].validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    var validateCheckPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.userInput.passWord) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      userInput: {
        userName: '',
        passWord: '',
        checkPass: ''
      },
      userInputRules: {
        userName: [{ validator: validateUserName, trigger: 'blur' }],
        passWord: [{ validator: validatePass, trigger: 'blur' }],
        checkPass: [{ validator: validateCheckPass, trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 跳转登录界面
    toLogin() {
      this.$router.push('/login')
    },
    // 登录
    toRegister() {
      this.$refs['user-input'].validate((valid) => {
        if (valid) {
          // 表单填写完整，进行登录校验
          register({
            userName: this.userInput.userName,
            password: this.userInput.passWord
          }).then(res => {
            this.$message.success('恭喜您注册成功！')
            this.$router.push('/login')
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.sys-register {
  height: 100%;
  background-image: url(https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic2.zhimg.com%2Fv2-5b3bcafc7b1422c2719ea0cd584e8ef5_r.jpg&refer=http%3A%2F%2Fpic2.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649907674&t=1c4ee0929cb392de23b027c632cdb014);
  opacity: 0.7;
  position: relative;
  .register-wrap {
    width: 500px;
    height: 440px;
    background: rgba(38,43,54,.9);
    position: absolute;
    left: 0;
    top: 0;
    bottom: 100px;
    right: 0;
    margin: auto;
    border-radius: 3px;
    padding: 0 40px;
    box-sizing: border-box;
  }
}
.register-head {
  color: #e7c43e;
  text-align: center;
  font-size: 24px;
  margin-top: 50px;
}
.register-input {
  padding-top: 30px;
  /deep/ .el-form-item__label {
    color: #fff;
  }
}
.to-register {
  text-align: right;
  span {
    color: #e7c43e;
    cursor: pointer;
    &:hover {
      color: #007aff;
    }
  }
}
.register-btn {
  width: 100%;
  margin-top: 15px;
  font-size: 18px;
}
</style>
